package com.raidpoints.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PointsServ extends HttpServlet{
	
	public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
		PrintWriter out = res.getWriter();
		
		Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/Los_Angeles"));
		
		out.println("-- LO Data Export");
		out.println("LeftoversDB = {");
		out.println("\tExportInfo = {");
		out.println("\t\tTime = \"" + new SimpleDateFormat("MM/dd/yy HH:mm:ss").format(cal.getTime()) + " -0800\"");
		out.println("\t},");
		out.println("\tPointsPools = {");
		out.println("\t\t[\"Cataclysm\"] = {");
		out.println("\t\t\tDesc = \"\",");
		out.println("\t\t\tPoints = {");
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			//Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/rp","root","");
		    Connection con = DriverManager.getConnection("jdbc:mysql://" + RaidPointsServiceImpl.loginfo.get("db"),RaidPointsServiceImpl.loginfo.get("dbuser"),RaidPointsServiceImpl.loginfo.get("dbpass"));
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT characters.name, points.points FROM characters INNER JOIN points on points.char_id=characters.id AND points.pool_id=1");
			while(rs.next()){
				out.println("\t\t\t\t[\"" + rs.getString("characters.name") + "\"] = " + rs.getDouble("points.points") + ",");
			}
			con.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		out.println("\t\t\t}");
		out.println("\t\t}");
		out.println("\t}");
		out.println("}");
		out.close();
	}

}
